package com.ycy.create;

import com.ycy.BaseNoListenerTest;
import org.flowable.engine.*;
import org.flowable.engine.repository.Deployment;
import org.flowable.engine.runtime.ProcessInstance;
import org.flowable.task.api.Task;
import org.junit.Test;

import java.util.Arrays;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

public class MultiCreateDemo extends BaseNoListenerTest {

  @Test
  public void creat0() {
    // 创建Flowable流程引擎 Create Flowable process engine
    ProcessEngine processEngine = cfg.buildProcessEngine();
    // 获取Flowable服务 Get Flowable repositoryService
    RepositoryService repositoryService = processEngine.getRepositoryService();
    Deployment deploy = repositoryService.createDeployment().addClasspathResource("bp/MoreLiving.bpmn20.xml").deploy();
    System.out.println(deploy.getId());
  }

  @Test
  public void create1() {

    // 通过数据库链接信息         创建Flowable流程引擎 Create Flowable process engine
    ProcessEngine processEngine = cfg.buildProcessEngine();
    // 获取Flowable服务 Get Flowable repositoryService
    RepositoryService repositoryService = processEngine.getRepositoryService();
    // 获取Flowable服务 Get Flowable runtimeService
    RuntimeService runtimeService = processEngine.getRuntimeService();
    // 获取taskService对象 Get the first task
    TaskService taskService = processEngine.getTaskService();

    HashMap<String, Object> map = new HashMap<>();
    //定义的人员列表4人
    String[] v = {"shareniu1", "shareniu2", "shareniu3", "shareniu4"};
    map.put("per", "bbb");
    map.put("money", "1111");
    map.put("assigneeList", Arrays.asList(v));
    ProcessInstance pi = runtimeService.startProcessInstanceByKey("living", map);

    List<Task> list = taskService.createTaskQuery().processInstanceId(pi.getId()).list();
    System.out.println(list.size());
    int i = 0;

    for (Task task : list) {
      i = i + 1;
      System.out.println("==========================所有节点name is =  " + task.getName());
      System.out.println("==========================所有节点id is =  " + task.getId());
      System.out.println("============ i =" + i);
      //变相判断已经二人提交 之后人员不提交
      if (i < 3) {
        System.out.println("================== 提交 节点 id is=" + task.getId());
//                taskService.complete(task.getId());
        //流程 通滚
//                Map<String, Object> variables = taskService.getVariables(task.getId());
//                System.out.println("variables " + variables);
        //获取局部变量
        Map<String, Object> variables = taskService.getVariablesLocal(task.getId());
        System.out.println("variables " + variables);
        System.out.println(task.getTaskLocalVariables());
      }
    }
    //判断值为 50% 所以提交人达到2人 会签节点即可通过
    Task task = taskService.createTaskQuery().processInstanceId(pi.getId()).singleResult();
    System.out.println("====================================================================================");
    //验证是否已通过
    System.out.println("===================task id is=" + task.getId());
    System.out.println("===================task name is=" + task.getName());

  }

  //2523
  public void test(){
    TaskService taskService = processEngine.getTaskService();
    taskService.complete("2523");
  }

}
